package com.shengqi.xinnongren.work.ui.user.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.user.model.ProvinceCityBean;
import com.shengqi.xinnongren.work.ui.user.view.AddAddressView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/6/25.
 */
public class AddAddressPresenter extends BasePresenter<AddAddressView> {

    public void getProvinceCityList() {
        JSONObject aesJsonData = Constants.getJsonData(null, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getProvinceCityCounty(requestBody), respond -> {
            HttpRespond<String> httpResult = Global.getHttpResult(respond);
            if (httpResult.result == 1) {
                List<ProvinceCityBean> list = new Gson().fromJson(httpResult.data, new TypeToken<List<ProvinceCityBean>>() {
                }.getType());
                getView().showProvinceCityList(list);
            } else
                getView().onRequestFailed(httpResult.message);
        });
    }

    public void addAddress(String edtName, String edtPhone, String provinceId, String cityId, String countyId, String edtAddressInfo, String idDef) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("name", edtName);
        map.put("mobile", edtPhone);
        map.put("province", provinceId);
        map.put("city", cityId);
        map.put("county", countyId);
        map.put("address", edtAddressInfo);
        map.put("isDefault", idDef);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().saveAddress(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().showAddAddressRes(respond);
            } else
                getView().onRequestFailed(respond.message);
        });
    }

    public void editAddress(String id, String edtName, String edtPhone, String provinceId, String cityId, String countyId, String edtAddressInfo, String idDef) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("addressId", id);
        map.put("name", edtName);
        map.put("mobile", edtPhone);
        map.put("province", provinceId);
        map.put("city", cityId);
        map.put("county", countyId);
        map.put("address", edtAddressInfo);
        map.put("isDefault", idDef);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().editAddress(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().showEditAddressRes(respond);
            } else
                getView().onRequestFailed(respond.message);
        });

    }
}
